<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function f(x){
            x.value= x.value.trim();
        }
    </script>
</head>
<body>
请输入:<input type="text" onkeyup="f(this)"/>
<br/>
自动过滤字符串左右空格
</body>
</html>